package cn.colins.consumer.filter;

import com.alibaba.fastjson.JSON;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.registry.integration.RegistryDirectory;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.cluster.Router;
import org.apache.dubbo.rpc.cluster.RouterFactory;
import org.apache.dubbo.rpc.cluster.router.AbstractRouter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author czl
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2023/5/8
 */
public class MyRouter extends AbstractRouter {
    public MyRouter(URL url){
        setUrl(url);
    }

    private final static Logger log= LoggerFactory.getLogger(MyRouter.class);

    @Override
    public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
        log.info("url:{}",url);
        // 根据已有条件过滤invokers 返回即可
        invokers.stream().filter(invoker->{
            log.info("Provider id: {}",invoker.getUrl().getHost());
            log.info("Provider port: {}",invoker.getUrl().getPort());
            log.info("Provider protocol: {}",invoker.getUrl().getProtocol());
            log.info("Provider methods : {}",invoker.getUrl().getParameter("methods", Collections.emptyList()));
            log.info("Provider Parameter: {}",invoker.getUrl().getParameters());
            return true;
        }).collect(Collectors.toList());
        return invokers;
    }
}
